package com.xinrui.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;

import com.xinrui.sunnyday.constant.MessageConstant;
import com.xinrui.sunnyday.constant.RedisMessageConstant;
import com.xinrui.sunnyday.entity.OrderInfo;
import com.xinrui.sunnyday.entity.Result;
import com.xinrui.sunnyday.exception.SunnydayException;
import com.xinrui.sunnyday.pojo.Order;
import com.xinrui.sunnyday.service.OrderService;
import com.xinrui.sunnyday.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderMobileController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;

    @PostMapping("/submit")
    public Result submit(@RequestBody Map<String,String> paraMap) throws SunnydayException {
        System.out.println("param = " + paraMap);
        //获取用户的手机号
        String telephone =  paraMap.get("telephone");
        //获取验证码的key
        String key = RedisMessageConstant.SENDTYPE_ORDER + "_" + telephone;
        //获取redis的验证码
        String codeInRedis = jedisPool.getResource().get(key);
        //获取前端传递过来的验证码
        String validateCode =  paraMap.get("validateCode");
        //判断redis里的验证码是否为空
        if (StringUtils.isEmpty(codeInRedis)){
            //为空 没有值
            //重新发送
            return new Result(false,"请重新获取验证码");
        }
        //判断前端获取到输入的验证码是否与redis的验证码一致
        if (!codeInRedis.equals(validateCode)){
            //不一致 重新输入
            return new Result(false,MessageConstant.VALIDATECODE_ERROR);
        }
        //移除redis的验证码
        //测试中注释掉
//        jedisPool.getResource().del(key);
        //信息判断完成
        //设置为微信预约
//        param.put("orderType",Order.ORDERTYPE_WEIXIN);
        Order order = orderService.orderSubmit(paraMap);
        return new Result(true, MessageConstant.ORDER_SUCCESS,order);
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        Map<String,Object> map = orderService.findById4Detail(id);
        return new Result(true,MessageConstant.ORDER_SUCCESS,map);
    }
}
